<template>
    <Drawer :title="title" width="800" v-model="drawer" :mask-closable="false" @on-close="closeDrawer">
        <div class="temeList">
            <Row class="pad16" type="flex">
                <Col>
                <div style="margin-bottom: 5px">
                    <strong style="font-size: 16px">{{ ruleForm.name }}</strong>
                    <span class="bor ivu-inline-block">{{ ruleForm.patient_card_id }}</span>
                    <span class="cols" @click="editDetail">查看治疗记录</span>
                    <Button class="ivu-ml-8" v-if="hqeditsurplus" @click="editsurplusfun" type="primary">编辑剩余量</Button>
                </div>
                <div>
                    <span class="cl6 ivu-inline-block">{{ ruleForm.mobile }}</span>
                    <span class="sign-label gender ivu-inline-block">{{ ruleForm.gender }}
                        <span>{{ ruleForm.age }}岁</span></span>
                    <span class="sign-label">上次治疗时间：{{ ruleForm.recently_clinic_time }}</span>
                </div>
                </Col>
                <Col class="colLe">
                <div class="ivu-inline-block">
                    <div class="sign-label">本次治疗</div>
                    <strong>{{currentTreatNum}}</strong>
                </div>
                <div class="ivu-inline-block">
                    <div class="sign-label">上次治疗</div>
                    <strong>{{ Number(ruleForm.recently_clinic_num) + Number(ruleForm.recently_surplus_num) }}</strong>
                </div>
                <div class="ivu-inline-block">
                    <div class="sign-label">剩余</div>
                    <strong>{{ Number(ruleForm.total_num) + Number(ruleForm.help_num) }}</strong>
                </div>
                </Col>
            </Row>
            <Row class="rowTime">
                <div class="ivu-clearfix">
                    <Col span="8" class="clearClean">
                    <DatePicker :editable="false" type="datetime" v-width="'100%'" v-model="createData.time"
                        :options="dateOption" placeholder="日期选择" :clearable="false" format="yyyy-MM-dd HH:mm" />
                    </Col>
                </div>
            </Row>
        </div>
        <!--    模板1 308   -->
        <div class="temeList" v-if="ruleForm.template === '1'||ruleForm.template === '6'">
            <div class="rowTime">
                <div class="bor4">
                    <Row :gutter="12" class="maxText">
                        <Col span="5">大部位</Col>
                        <Col span="5">细节部位</Col>
                        <Col span="5">剂量/时间</Col>
                        <Col span="5">数量</Col>
                        <Col span="4">操作</Col>
                    </Row>
                    <Form ref="models" id="models" :model="models">
                        <div class="ivu-mb-0" v-for="(item, index) in models.lists" :key="'item' + index">
                            <Row :gutter="12">
                                <Col span="5">
                                <FormItem :prop="'lists.' + index + '.body_parts_large_name'">
                                    <!-- <Select :class="item.active ? 'active' : ''" @on-change="selectChange(item, index)" :clearable="false" v-model="item.body_parts_large_name" placeholder="请选择">
                                            <Option  v-for="items in position" :key="items.id" :value="items.body_parts_name">{{ items.body_parts_name }}</Option>
                                        </Select> -->
                                    <div :class="item.active ? 'active hqselectgaidiv' : 'hqselectgaidiv'"
                                        @click="selectbig(item, index)">
                                        {{item.body_parts_large_name || '请选择'}}</div>
                                </FormItem>
                                </Col>
                                <Col span="5">
                                <FormItem :prop="'lists.' + index + '.body_parts_small_name'">
                                    <!-- <Select :class="item.active ? 'active' : ''" :clearable="false" v-model="item.body_parts_small_name" placeholder="请选择">
                                            <Option v-for="items in childrenOption[index]" :key="items.id" :value="items.body_parts_name">{{ items.body_parts_name }}</Option>
                                        </Select> -->
                                    <div :class="item.active ? 'active hqselectgaidiv' : 'hqselectgaidiv'"
                                        @click="selectsmall(item, index)">
                                        {{item.body_parts_small_name || '请先选择大部位'}}</div>
                                </FormItem>
                                </Col>
                                <Col span="5">
                                <FormItem>
                                    <div class="nums">
                                        <Input @on-enter="focus('num',index)" type="number" :ref="`dose${index}`" :class="item.active ? 'active' : ''" @on-focus="addListss(item)"
                                            @on-blur="numberInput(item.dose, index, 'dose')" @on-change="item.dose = Number($event.target.value.replace(/\D+/, ''))"
                                            v-width="'100%'" class="InputNumber" :min="1" :clearable="false"
                                            v-model="item.dose" placeholder="请输入"/>
                                        <Button :class="item.active ? 'mj1' : 'mj'" slot="append">/mj</Button>
                                    </div>
                                </FormItem>
                                </Col>
                                <Col span="5">
                                <FormItem>
                                    <Input @on-enter="focus('large',index)" type="number" :ref="`num${index}`" @on-focus="addListss(item)" :class="active ? 'actives' : ''"
                                        @on-blur="numberInput(item.num, index, 'num')" @on-change="item.num = Number($event.target.value.replace(/\D+/, ''))"
                                        v-width="'100%'" class="numt" :min="1" :clearable="false" v-model="item.num"
                                        placeholder="请输入" />
                                </FormItem>
                                </Col>
                                <Col span="4">
                                <Button type="primary" @click="addLists(index)" icon="md-add"></Button>
                                <Button v-if="index !== 0" type="primary" class="ivu-ml-8" @click="removeLists(index)"
                                    icon="md-remove"></Button>
                                </Col>
                            </Row>
                        </div>
                    </Form>
                    <Row class="remarkMax">
                        <Input v-model="createData.remark" type="textarea" placeholder="请输入备注内容" />
                    </Row>
                </div>
            </div>
        </div>
        <!--    模板2 3 4   -->
        <div class="temeList"
            v-else-if="ruleForm.template === '2'||ruleForm.template === '3'||ruleForm.template === '4'">
            <div class="rowTime">
                <div class="bor4">
                    <div class="ivu-mb-8">
                        <Form ref="form" :model="twoList" :label-width="90" :rules="rule">
                            <Row :gutter="12">
                                <Row v-if="ruleForm.template === '4'">
                                    <Col span="12" style="padding: 0 !important;">
                                    <FormItem label="执行部位" prop="body_parts_large_name">
                                        <Select :clearable="false" v-model="twoList.body_parts_large_name"
                                            placeholder="请选择">
                                            <Option v-for="items in halfCabin" :key="items.id"
                                                :value="items.bc_parts_name">{{ items.bc_parts_name }}</Option>
                                        </Select>
                                    </FormItem>
                                    </Col>
                                </Row>
                                <Row>
                                    <Col span="12" v-if="ruleForm.template === '3' || ruleForm.template === '2'">
                                    <FormItem label="执行数量" prop="num">
                                        <InputNumber :formatter="ins" v-width="'100%'"
                                            @input="twoList.num = Number($event.target.value.replace(/\D/g, ''))"
                                            @on-change="numberInput(twoList.num)" class="InputNumber" :min="1"
                                            :clearable="false" v-model="twoList.num" placeholder="请输入" type="number">
                                        </InputNumber>
                                    </FormItem>
                                    </Col>
                                </Row>
                                <Row>
                                    <Col v-if="ruleForm.template === '2'" span="12" style="padding: 0 !important;">
                                    <FormItem label="剂量/时间" prop="dose">
                                        <div class="nums">
                                            <InputNumber :step="0.01" :formatter="insing" v-width="'100%'"
                                                class="InputNumber" :clearable="false" v-model.number="twoList.dose"
                                                placeholder="请输入" type="number">
                                            </InputNumber>
                                            <Button style="top: 1px" class="mj" slot="append">/mj</Button>
                                        </div>
                                    </FormItem>
                                    </Col>
                                </Row>
                                <Row>
                                    <Col v-if="ruleForm.template === '4'" span="12" style="padding: 0 !important;">
                                    <FormItem label="剂量/时间" prop="dose">
                                        <div class="nums">
                                            <InputNumber :step="0.01" :formatter="insing" v-width="'100%'"
                                                class="InputNumber" :clearable="false" v-model.number="twoList.dose"
                                                placeholder="请输入" type="number">
                                            </InputNumber>
                                            <Button style="top: 1px" class="mj" slot="append">/ms</Button>
                                        </div>
                                    </FormItem>
                                    </Col>
                                </Row>
                            </Row>
                        </Form>
                    </div>
                </div>
            </div>
        </div>
        <div class="temeList" v-else>
            <div class="rowTime">
                <div class="bor4">
                    <div class="ivu-mb-8">
                        <Form ref="form" :model="twoList" :label-width="90" :rules="rule">
                            <Row :gutter="12">
                                <Row>
                                    <Col span="12">
                                    <FormItem label="执行数量" prop="num">
                                        <InputNumber :formatter="ins" v-width="'100%'"
                                            @on-change="numberInput(twoList.num)" class="InputNumber" :min="1"
                                            :clearable="false" v-model="twoList.num" placeholder="请输入" type="number">
                                        </InputNumber>
                                    </FormItem>

                                    <FormItem label="执行时长" prop="dose">
                                        <div class="nums">
                                            <InputNumber v-width="'98%'" @on-change="numberInput(twoList.dose)"
                                                class="InputNumber" :step="0.01" :formatter="insing" :clearable="false"
                                                v-model="twoList.dose" placeholder="请输入" type="number">
                                            </InputNumber>
                                            <Button style="top: 1px" class="mj" slot="append">/min</Button>
                                        </div>
                                        <!-- <InputNumber v-width="'100%'" v-model="twoList.nums"></InputNumber> -->
                                    </FormItem>
                                    </Col>
                                </Row>
                            </Row>
                        </Form>
                    </div>
                </div>
            </div>
        </div>
        <div class="temeList">
            <Row class="rowTime">
                <div class="ivu-clearfix">
                    <Col span="8" class="clearClean">
                    <div class="temeList">
                        <span class="sapn">操作人：</span>
                        <RadioGroup v-model="disabledSingle" type="button" class="RadioGroup" @on-change="change">
                            <Radio style="margin-right: 20px;margin-bottom: 5px" v-for="item in realData" :key="item.id"
                                :label="item.id">{{item.realname}}</Radio>
                        </RadioGroup>
                    </div>
                    </Col>
                </div>
            </Row>
        </div>
        <div class="temeList">
            <div class="rowTime">
                <Row type="flex" class="clickBtn">
                    <Col>
                    <Button style="margin-right: 20px" type="primary" :loading="btnLoading"
                        @click="handleSubmit(0,( ruleForm.template === '1'|| ruleForm.template ==='6') ? 'models' : 'form')">完成治疗</Button>
                    <Button class="ivu-mr-8" @click="handleSubmit(1)">取消</Button>
                    </Col>
                </Row>
            </div>
        </div>
        <Modal v-model="modal1" :mask-closable="false" title="提交确认" draggable
            @on-ok="ok((ruleForm.template === '1' || ruleForm.template ==='6')? 'models' : 'form')" @on-cancel="cancel">
            <span style="margin-right: 10px;">{{ruleForm.name}}</span> <span style="margin-right: 10px"
                class="cl6 ivu-inline-block">{{ ruleForm.mobile }}</span> <span
                class="sign-label gender ivu-inline-block">{{ ruleForm.gender }}
                <span>{{ ruleForm.age }}岁</span></span><br>
            <span style="margin-right: 10px;" class="top">本次执行数量：</span> <span
                style="margin-right: 10px;font-weight: 700" class="top">{{num}}</span><span style="margin-right: 10px;"
                class="top">剩余数量：</span> <span style="font-weight: 700"
                class="top">{{ Number(ruleForm.total_num) + Number(ruleForm.help_num) - Number(currentTreatNum)}}</span>
        </Modal>
        <Modal width="660" footer-hide v-model="selectfun" draggable :title="selecttitle?'选择大部位':'选择细节部位'" @on-cancel="cancels">
            <div class="selectbutton">
                <template>
                    <!--  :type="selectshow === items.body_parts_name?'primary':null" -->
                    <Button @click="selectIndex(items)"
                        v-for="items in selectlist" :key="items.id">{{ items.body_parts_name }}</Button>
                </template>
            </div>
        </Modal>
        <Modal width="660" v-model="editsurplus" draggable :title="`${ruleForm.name}-编辑剩余量`">
            <Form inline :label-width="90">
                <FormItem label="正常：">
                    <InputNumber :max="9999" :min="-9999" :step="1" :formatter="ins" v-model="editsurplusparams.total_num"></InputNumber>个
                </FormItem>
                <FormItem label="援助：">
                    <InputNumber :max="9999" :min="-9999" :step="1" :formatter="ins" v-model="editsurplusparams.help_num"></InputNumber>个
                </FormItem>
            </Form>
            <div slot="footer">
                <Button @click="editsurpluscancel">取消</Button>
                <Button type="primary" :loading="subloading" @click="editsurplussub">确定</Button>
            </div>
        </Modal>
    </Drawer>
</template>
<script>
    import qs from 'qs'
    import { GetPosition, GetHalfCabin, GetPersonDetail, postTemplate, updateTemplate, GetOperatorList, patientClinicList, clinictreatmentmanageupdateclinicnum } from '@/api/treatment'
    import dayjs from 'dayjs'
    export default {
        props: {
            isEdit: {
                type: String,
                default: ''
            }
        },
        name: 'templateFore',
        data() {
            return {
                editsurplus: false,
                disabledSingle: '',
                createData: {
                    time: new Date(),
                    remark: ''
                },
                drawer: false,
                subloading: false,
                ruleForm: {},
                title: '',
                num: 0,
                models: {
                    lists: [

                    ]
                },
                dateOption: {
                    disabledDate(pick) {
                        const curDate = new Date();
                        let reVal = 0;
                        const startTime = new Date(pick).getTime()
                        const endTime = curDate.getTime() || curDate.valueOf()
                        const num = 3 * 24 * 61 * 61 * 1000
                        if ((endTime - startTime) > num || pick > curDate) {
                            reVal = 1
                        }
                        return reVal
                    }
                },
                twoList: {
                    dose: 1,
                    num: 1,
                    body_parts_large_name: ''
                },
                halfCabin: [],
                position: [],
                childrenOption: [],
                rule: {
                    body_parts_large_name: [{ required: true, message: '请选择部位', trigger: 'change' }],
                    dose: [
                        {
                            required: true,
                            validator: (rule, value, callback) => {
                                if (value === '' || value === null || value === undefined) {
                                    callback(new Error('请输入'))
                                } else if (value === 0) {
                                    callback(new Error('请输入大于0的整数'))
                                } else {
                                    callback()
                                }
                            },
                            trigger: 'change'
                        }
                    ],
                    num: [{
                        required: true,
                        validator: (rule, value, callback) => {
                            if (value === '' || value === null || value === undefined) {
                                callback(new Error('请输入'))
                            } else if (value === 0) {
                                callback(new Error('请输入大于0的整数'))
                            } else {
                                callback()
                            }
                        },
                        trigger: 'change'
                    }]
                },
                type: '',
                btnLoading: false,
                modal1: false,
                selectfun: false,
                selecttitle: false,
                realData: [],
                id: '',
                wait_type: '',
                body_parts_large_name: '',
                body_parts_small_name: '',
                active: true,
                selectshow: '',
                selectlist: [],
                selectdatalist: [],
                selectdataindex: '',
                hqeditsurplus: false,
                state: {},
                editsurplusparams: {
                    total_num: null,
                    help_num: null,
                    patient_card_id: '',
                    clinic_project_id: '',
                }
            }
        },
        created() {
            this.state = JSON.parse(localStorage.getItem('poweredit'));
            for (let item of this.state) {
                if (item.name === '编辑剩余量') {
                    this.hqeditsurplus = item.bool
                }
            }
            this.active = true
        },
        watch: {
            isEdit(value) {
                
            }
        },
        computed: {
            currentTreatNum() {
                let total = 0
                if (this.ruleForm.template === '1'||this.ruleForm.template ==='6') {
                    this.models.lists.forEach((t, index) => {
                        // if (!t.active && t.body_parts_large_name !== '') {
                        if (t.num !== null) {
                            total += parseInt(t.num)
                        }
                        // }
                    })
                } else {
                    if (this.twoList.num) {
                        total = parseInt(this.twoList.num)
                    } else {
                        total = 0
                    }
                }
                return total
            }
        },
        mounted() {
            this.getPosition()
            this.getHalfCabin()
        },
        methods: {
            closeDrawer() {
                this.$emit('closeDrawer')
            },
            editsurplusfun() {
                console.log(this.ruleForm)
                this.editsurplusparams.total_num = +this.ruleForm.total_num
                this.editsurplusparams.help_num = +this.ruleForm.help_num
                this.editsurplusparams.patient_card_id = this.ruleForm.patient_card_id
                this.editsurplusparams.clinic_project_id = this.ruleForm.clinic_project_id
                this.editsurplus = true
            },
            editsurplussub() {
                this.subloading = true
                clinictreatmentmanageupdateclinicnum(this.editsurplusparams).then(res=>{
                    this.$Message.success('编辑剩余量成功')
                    this.ruleForm.total_num = +this.editsurplusparams.total_num
                    this.ruleForm.help_num = +this.editsurplusparams.help_num
                    this.ruleForm.patient_card_id = this.editsurplusparams.patient_card_id
                    this.ruleForm.clinic_project_id = this.editsurplusparams.clinic_project_id
                    this.subloading = false
                    this.editsurpluscancel()
                }).catch(() => {
                    this.subloading = false
                    this.$Message.error('编辑剩余量失败')
                })
            },
            editsurpluscancel() {
                this.editsurplusparams = {
                    total_num: null,
                    help_num: null,
                    patient_card_id: '',
                    clinic_project_id: '',
                }
                this.editsurplus = false
            },
            change(id) {
                this.id = id
            },
            ok(name) {
                if (this.btnLoading) return false
                else {
                    this.$refs[name].validate((valid) => {
                        if (valid) {
                            this.postOne()
                        }
                    })
                }
            },
            cancel() {
                this.num = 0
            },
            numberInput(e, index, key, item) {
                this.num = e
                if (!e) {
                    this.$set(this.models.lists[index], key, null)
                }
            },
            // 获取身体部位
            getPosition() {
                const config = {
                    page: 1,
                    pageSize: 500,
                    status: 1
                }
                GetPosition(config).then(res => {
                    this.position = res.list
                })
            },
            // 半仓311部位
            getHalfCabin() {
                GetHalfCabin({ status: 1 }).then(res => {
                    this.halfCabin = res.list
                })
            },
            postOne() {
                this.btnLoading = true
                const temp = this.ruleForm.template
                console.log(this.ruleForm.template,'ruleForm.template')
                const time = dayjs(this.createData.time).format('YYYY-MM-DD HH:mm')
                let content = []
                const clinic_project_id = this.ruleForm.clinic_project_id
                const treatment_room_id = this.ruleForm.treatment_room_id
                const body_parts_large_name = this.twoList.body_parts_large_name // 执行部位
                const num = this.twoList.num || 1 // 执行部位
                const dose = this.twoList.dose || 1
                const remark = this.createData.remark
                const patient_card_id = this.ruleForm.patient_card_id // 诊疗号
                let config = {
                    time: time,
                    template: temp,
                    patient_card_id: patient_card_id,
                    clinic_project_id: clinic_project_id,
                    treatment_room_id: treatment_room_id,
                    wait_type: this.wait_type
                }
                if (temp === '1' ||temp === '6') { // 308 //纳晶
                    for (let i = 0; i < this.models.lists.length; i++) {
                        if (this.models.lists[i].body_parts_large_name !== '' && this.models.lists[i].body_parts_small_name !== '' && this.models.lists[i].active === false) {
                            this.models.lists[i].dose = parseInt(this.models.lists[i].dose) || 1
                            this.models.lists[i].num = parseInt(this.models.lists[i].num)
                            this.models.lists[i].id = 0
                            content.push(this.models.lists[i])
                        } else if (this.models.lists[i].body_parts_large_name !== '' && this.models.lists[i].body_parts_small_name !== '' && this.models.lists[i].active === undefined) {
                            this.models.lists[i].dose = parseInt(this.models.lists[i].dose) || 1
                            this.models.lists[i].num = parseInt(this.models.lists[i].num)
                            content.push(this.models.lists[i])
                        }
                    }
                    config.content = JSON.stringify(content)
                    config.remark = remark
                    config.operate_id = this.id
                    if (content.length === 0) {
                        this.$Message.warning('请至少录入一组数据')
                        this.btnLoading = false
                        return false
                    }
                } else if (temp === '2') {
                    config.operate_id = this.id
                    config.num = parseInt(num)
                    config.dose = this.insing(dose)
                } else if (temp === '3') {
                    config.operate_id = this.id
                    config.num = parseInt(num)
                } else if (temp === '4') {
                    config.operate_id = this.id
                    config.dose = this.insing(dose)
                    config.body_parts_large_name = body_parts_large_name
                } else if (temp === '5') {
                    config.operate_id = this.id
                    config.num = parseInt(num)
                    config.dose = this.insing(dose)
                    config.body_parts_large_name = body_parts_large_name
                }
                if (this.type === 1) { // 录入
                    delete config.id
                    postTemplate(qs.stringify({ ...config })).then(res => {
                        if (res) {
                            this.$emit('putMessage')
                            this.$Message.success('治疗信息录入成功')
                            this.resetResult()
                        } else {
                            this.$Message.error('治疗信息录入失败')
                        }
                    }).finally(() => {
                        this.$nextTick(() => {
                            this.btnLoading = false
                        })
                    })
                } else if (this.type === 2) {
                    config.id = this.ruleForm.id
                    config.dose = config.dose
                    config.num = parseInt(config.num)
                    updateTemplate(qs.stringify({ ...config })).then(res => {
                        if (res) {
                            this.$emit('putMessage')
                            this.resetResult()
                            this.$Message.success('治疗信息修改成功')
                        } else {
                            this.$Message.error('治疗信息修改失败')
                        }
                    }).finally(() => {
                        this.$nextTick(() => {
                            this.btnLoading = false
                        })
                    })
                }
                this.num = 0
            },
            editDetail() {
                this.$emit('edit', this.ruleForm.patient_card_id, this.ruleForm.template)
            },
            templateList(row, template, title, status, id, type, wait_type) {
                if (template === '1'||template === '6') {
                    console.log(id)
                    const config = {
                        clinic_project_id: id.clinic_project_id,
                        keywords: row.name
                    }
                    console.log('==========>', config)
                    patientClinicList(config).then(res => {
                        this.active = true
                        for (let i = 0; i < res.length; i++) {
                            for (let y = 0; y < res[i].treatment_detail.length; y++) {
                                for (let j = 0; j < this.position.length; j++) {
                                    if (res[i].patient_card_id === row.patient_card_id) {
                                        if (this.position[j].body_parts_name === res[i].treatment_detail[y].body_parts_large_name) {
                                            this.$set(this.childrenOption, y, [])
                                            if (this.position[j].children && this.position[j].children.length > 0) {
                                                this.$set(this.childrenOption, y, (this.position[j].children || []))
                                            } else {
                                                this.childrenOption[y].push(this.position[j])
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        this.$nextTick(() => {
                            for (let i = 0; i < res.length; i++) {
                                if (res[i].patient_card_id === row.patient_card_id) {
                                    if (res[i].treatment_detail.length === 0) {
                                        this.models.lists[i].body_parts_large_name = ''
                                        this.models.lists[i].body_parts_small_name = ''
                                        this.models.lists[i].dose = 1
                                        this.models.lists[i].num = null
                                        this.models.lists[i].active = true
                                    } else {
                                        this.models.lists = res[i].treatment_detail || ''
                                        for (let y = 0; y < this.models.lists.length; y++) {
                                            this.models.lists[y].num = null
                                            this.$set(this.models.lists[y], 'active', true)
                                        }
                                    }
                                }
                            }
                        })
                    })
                }
                this.wait_type = wait_type
                this.num = 0
                GetOperatorList().then(res => {
                    if (res instanceof Array) {
                        this.realData = res
                    } else {
                        this.realData = res.attrArray
                    }
                    if (this.realData.length === 1) {
                        this.disabledSingle = this.realData[0].id
                        this.id = this.disabledSingle
                    } else {
                        // let isOperator = res.attrArray.find(vv=>vv.realname == this.isEdit)
                        this.realData = res.attrArray
                        this.disabledSingle = res.operatorId
                        this.id = res.operatorId
                        // if(isOperator){
                        //     this.disabledSingle = isOperator.id
                        //     this.id = isOperator.id
                        // }else{
                        //     this.disabledSingle = res.operatorId
                        //     this.id = res.operatorId
                        // }
                    }
                })
                this.drawer = true
                console.log(this.drawer,'this.drawer')
                this.type = type
                this.title = title + '  -   ' + '录入治疗'
                this.models.lists = [
                    {
                        body_parts_large_name: '',
                        body_parts_small_name: '',
                        dose: 1,
                        num: 0,
                        id: 0
                    }
                ]
                this.childrenOption = []
                this.ruleForm = {}
                this.createData.time = new Date()
                this.createData.remark = ''
                this.twoList.dose = 1
                this.twoList.num = 1
                this.twoList.body_parts_large_name = ''
                if (status === 1) { // 需要请求获取当前患者信息
                    this.getPersonal(row, template, id)
                } else if (status === 2) {
                    this.ruleForm = row
                    console.log( this.ruleForm ,row,'row')
                    this.ruleForm.template = template
                    this.ruleForm.clinic_project_id = id.clinic_project_id
                    this.ruleForm.treatment_room_id = id.treatment_room_id
                }
            },
            async getPersonal(row, template, id) {
                const config = {
                    page: 1,
                    pageSize: 500,
                    status: 1
                }
                await GetPosition(config).then(res => {
                    this.position = res.list
                })
                // 获取个人信息
                GetPersonDetail({ id: row.id }).then(res => {
                    for (let i = 0; i < res.content.length; i++) {
                        for (let j = 0; j < this.position.length; j++) {
                            if (this.position[j].body_parts_name === res.content[i].body_parts_large_name) {
                                this.$set(this.childrenOption, i, [])
                                if (this.position[j].children && this.position[j].children.length > 0) {
                                    this.$set(this.childrenOption, i, (this.position[j].children || []))
                                } else {
                                    this.childrenOption[i].push(this.position[j])
                                }
                            }
                        }
                    }
                    this.$nextTick(() => {
                        this.models.lists = res.content
                    })
                    this.createData.remark = res.remark
                    this.ruleForm = {
                        ...res,
                        ...id,
                        template,
                        id: row.id
                    }
                    this.createData.time = new Date(res.recently_clinic_time)
                    if (this.ruleForm.template !== '1' ||this.ruleForm.template !== '6') {
                        this.twoList.dose = res.content[0].dose || 1
                        this.twoList.num = res.content[0].num || 1
                        this.twoList.body_parts_large_name = res.content[0].body_parts_large_name || ''
                    }
                })
            },
            addListss(e) {
                e.active = false
            },
            addLists(index) {
                this.active = false
                this.models.lists.splice(index+1,0,{
                    active: true,
                    body_parts_large_name: this.models.lists[index].body_parts_large_name,
                    body_parts_small_name: this.models.lists[index].body_parts_small_name,
                    dose: null,
                    num: null
                })
                // this.models.lists.push({
                //     active: true,
                //     body_parts_large_name: '',
                //     body_parts_small_name: '',
                //     dose: 1,
                //     num: null
                // })
                this.childrenOption.splice(index, 0, this.childrenOption[index])

            },
            removeLists(index) {
                this.models.lists.splice(index, 1)
                this.childrenOption.splice(index, 1)
            },
            resetResult() {
                this.models.lists = [
                    {
                        body_parts_large_name: '',
                        body_parts_small_name: '',
                        dose: 1,
                        num: null
                    }
                ]
                this.childrenOption = []
                this.ruleForm = {}
                this.createData.time = new Date()
                this.createData.remark = ''
                this.twoList.dose = null
                this.twoList.num = null
                this.twoList.body_parts_large_name = ''
                this.drawer = false
                this.disabledSingle = ''
            },
            handleSubmit(status, name) {
                // console.log(status, this.models.lists)
                this.num = 0
                if (this.id === '' || this.disabledSingle === '') {
                    this.$Message.info('请选择操作人');
                    return
                }
                if (this.ruleForm.template === '4') {
                    if (status === 1) { // 取消
                        this.resetResult()
                        this.num = 1
                    } else { // 保存
                        if (this.btnLoading) return false
                        else {
                            this.$refs[name].validate((valid) => {
                                if (valid) {
                                    this.postOne()
                                }
                            })
                        }
                    }
                } else if (this.ruleForm.template === '1'||this.ruleForm.template === '6') {
                    this.num = this.currentTreatNum
                    if (status === 1) { // 取消
                        this.active = true
                        this.resetResult()
                        this.num = 1
                    } else { // 保存
                        let contents = []
                        for (let i = 0; i < this.models.lists.length; i++) {
                            if (this.models.lists[i].body_parts_large_name !== '' && this.models.lists[i].body_parts_small_name !== '' && this.models.lists[i].active === false) {
                                contents.push(this.models.lists[i])
                            } else if (this.models.lists[i].body_parts_large_name !== '' && this.models.lists[i].body_parts_small_name !== '' && this.models.lists[i].active === undefined) {
                                contents.push(this.models.lists[i])
                            }
                        }
                        if (contents.length === 0) {
                            this.$Message.warning('请至少录入一组数据')
                            this.btnLoading = false
                            return false
                        }
                        for (let i = 0; i < this.models.lists.length; i++) {
                            let isActived = this.models.lists.some((treat, index) => {
                                return treat.active !== true
                            })
                            if (isActived === false) {
                                return this.$Message.info('请先激活一条数据')
                            } else {
                                this.modal1 = true
                            }
                            if (this.models.lists[i].active === true) {
                                return this.models.lists[i]
                            }
                            if ((this.models.lists[i].num != null || this.models.lists[i].num !== '') && this.models.lists[i].body_parts_small_name === '') {
                                this.modal1 = false
                                return this.$Message.info('请选择治疗部位')
                            } else {
                                this.modal1 = true
                            }
                            if (this.models.lists[i].dose == null || this.models.lists[i].dose === '') {
                                this.modal1 = false
                                return this.$Message.info('治疗计量/时间不能为空')
                            } else {
                                this.modal1 = true
                            }
                            if (this.models.lists[i].num == null || this.models.lists[i].num === '') {
                                this.modal1 = false
                                return this.$Message.info('治疗数量不能为空')
                            } else {
                                this.modal1 = true
                            }
                        }
                    }
                } else {
                    if (status === 1) { // 取消
                        this.resetResult()
                        this.num = 0
                    } else { // 保存
                        if (this.btnLoading) return false
                        else {
                            this.$refs[name].validate((valid) => {
                                if (valid) {
                                    this.postOne()
                                }
                            })
                        }
                    }
                }
            },
            // 数字输入框为正整数
            ins(e) {
                return parseInt(e)
            },
            insing(e) {
                let num = ('' + e).split('.')
                let nums = ''
                if (num.length > 1) {
                    if (num[1].length > 2) {
                        num[1] = num[1].substring(0, 2)
                    }
                    nums = num[0] + '.' + num[1]
                } else {
                    nums = num[0]
                }
                return nums
            },
            // 一级部位判断二级部位
            selectChange(item, index) {
                const id = item.body_parts_large_name
                item.body_parts_small_name = ''
                let t = {}
                let o = []
                for (let i = 0; i < this.position.length; i++) {
                    if (this.position[i].body_parts_name === id) {
                        t = this.position[i]
                        o = this.position[i].children || []
                        break
                    }
                }
                this.$nextTick(() => {
                    this.$set(this.childrenOption, index, [])
                    this.$nextTick(() => {
                        if (o.length === 0) {
                            this.childrenOption[index].push(t)
                            item.body_parts_small_name = item.body_parts_large_name
                        } else {
                            this.$set(this.childrenOption, index, o)
                        }
                    })
                })
            },
            selectIndex(items) {
                this.selectshow = items.body_parts_name
                if (this.selecttitle) {
                    this.models.lists[this.selectdataindex].body_parts_large_name = items.body_parts_name
                    this.selecttitle = false
                    const id = this.selectdatalist.body_parts_large_name
                    this.selectdatalist.body_parts_small_name = ''
                    let isSelect = this.position.find(e => e.body_parts_name == id)
                    this.$set(this.childrenOption, this.selectdataindex, [])
                    if (isSelect.children.length === 0) {
                        this.childrenOption[this.selectdataindex].push(isSelect)
                        this.selectdatalist.body_parts_small_name = this.selectdatalist.body_parts_large_name
                    } else {
                        this.$set(this.childrenOption, this.selectdataindex, isSelect.children)
                    }
                    this.selectlist = this.childrenOption[this.selectdataindex]
                    this.selectshow = ""
                } else {
                    this.models.lists[this.selectdataindex].body_parts_small_name = items.body_parts_name
                    this.$refs[`dose${this.selectdataindex}`][0].focus({preventScroll: true})
                    this.selectfun = false

                }
            },
            focus(name,index) {
                if (name === 'num') {
                    this.$refs[`${name}${index}`][0].focus({preventScroll: true})
                } else if (name === 'large') {
                    this.selectshow = ''
                    if (this.models.lists[index+1]) {
                        this.selectbig(this.models.lists[index+1], index+1)
                    }
                }
            },
            /**
             * @函数功能: 选择大部位
             * @参数1[item][对象]: 录入治疗单项
             * @参数2[index][数字]: 下表
             */
            selectbig(item, index) {
                this.selecttitle = true
                // this.selectdataindex为当前选择的第几行的index
                this.selectdataindex = index
                this.selectfun = true
                // position为大模块框选择的数据
                this.selectlist = this.position
                this.selectdatalist = item
            },
            /**
             * @函数功能: 选择部位细节
             * @参数1[item][对象]: 录入治疗单项
             * @参数2[index][数字]: 下表
             */
            selectsmall(item, index) {
                this.selecttitle = false
                this.selectdataindex = index
                this.selectfun = true
                this.selectdatalist = item
                this.selectlist = this.childrenOption[index]
            },
            oks() {
                if (this.selecttitle) {
                    return false
                    // console.log('大的')
                    // console.log(this.selectdatalist)

                    // const id = this.selectdatalist.body_parts_large_name
                    // this.selectdatalist.body_parts_small_name = ''
                    // let isSelect = this.position.find(e => e.body_parts_name == id)
                    // this.$set(this.childrenOption, this.selectdataindex, [])
                    // if (isSelect.children.length === 0) {
                    //     this.childrenOption[this.selectdataindex].push(isSelect)
                    //     this.selectdatalist.body_parts_small_name = this.selectdatalist.body_parts_large_name
                    // } else {
                    //     this.$set(this.childrenOption, this.selectdataindex, isSelect.children)
                    // }
                    // this.selectlist = this.childrenOption[this.selectdataindex]
                    // this.selecttitle = false
                } else {
                    // console.log(this.selectshow)
                    if (this.selectshow == '') {
                        this.$Message.warning('请先选择小部位')
                        return false
                    } else {
                        this.models.lists[this.selectdataindex].body_parts_small_name = JSON.parse(JSON.stringify(this.selectshow))
                        this.selectfun = false
                    }
                }

                this.selectshow = ''
            },
            cancels() {
                this.selectfun = false
                this.selectlist = []
            }
        }
    }
</script>

<style scoped lang="less">
    /deep/.ivu-drawer-body {
        padding: 0;
    }

    .sign-label {
        color: #666 !important;
    }

    .temeList {
        .pad16 {
            padding: 16px;
        }

        .colLe {
            margin-left: auto;
            display: flex;
            align-items: center;
            justify-content: center;
            height: 51px;
            text-align: center;

            .ivu-inline-block {
                margin: 0 12px;

                .sign-label {
                    margin-bottom: 8px;
                }
            }
        }

        .bor {
            padding: 1px 8px;
            border: 1px solid #ccc;
            border-radius: 12px;
            margin: 0 10px;
        }

        .cols {
            color: #30b862;
            cursor: pointer;
        }

        .gender {
            margin: 0 24px 0 12px;
        }

        strong,
        .cl6 {
            color: #333;
        }

        .rowTime {
            padding: 10px;
            background-color: #F2F2F2;
            box-sizing: border-box;

            .ivu-clearfix {
                background-color: #fff;
                padding: 16px;
                border-radius: 4px;
            }

            .clickBtn {
                margin-top: 50px;
                padding-bottom: 40px;
            }

            .bor4 {
                border-radius: 4px;
                background-color: #fff;
                padding: 16px;

                .maxText {
                    border-bottom: 1px solid #eee;
                    margin-bottom: 10px;
                    padding-bottom: 10px;
                }

                /deep/.ivu-mb-8:last-child {
                    margin-bottom: 0 !important;
                }

                .remarkMax {
                    margin-top: 26px;
                }

                /deep/.ivu-form {
                    .ivu-mb-0 {
                        .ivu-select {
                            .ivu-select-selected-value {
                                color: #ababab;
                            }
                        }
                    }
                }
            }

            .InputNumber {
                width: 100%;
                color: #999;
            }

            .nums {
                position: relative;

                .InputNumber,
                .numt {
                    width: 100%;
                }

                .InputNumber /deep/.ivu-input-number-handler-wrap {
                    right: 30px;
                }

                .mj {
                    padding: 0 5px;
                    background-color: #30b862;
                    color: #fff;
                    border-top-left-radius: 0;
                    border-bottom-left-radius: 0;
                    position: absolute;
                    right: 0;
                    top: 1px;
                    border-color: #30b862;
                }

                .mj1 {
                    padding: 0 5px;
                    background-color: #efeaea;
                    color: #fff;
                    border-top-left-radius: 0;
                    border-bottom-left-radius: 0;
                    position: absolute;
                    right: 0;
                    top: 1px;
                    border-color: #efeaea;
                }
            }
        }
    }

    .clearClean /deep/.ivu-picker-confirm .ivu-btn-default {
        display: none;
    }

    .RadioGroup {
        width: 500px !important;
        margin-left: 60px !important;
        margin-top: -25px;
    }

    .ivu-mr-8 {
        background: #cccccc;
        color: #ffffff;
    }

    .top {
        line-height: 60px !important;
    }

    .hqselectgaidiv {
        display: block;
        height: 32px;
        line-height: 30px;
        font-size: 14px;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        padding-left: 8px;
        padding-right: 24px;
        border-radius: 4px;
        border: 1px solid #dcdee2;
        color: #17233d;

        &:hover {
            border-color: #10b550;
        }
    }

    .active {
        color: #ababab;
    }

    .selectbutton {
        button {
            margin: 6px;
        }
    }
</style>